package cn.topevery.garden.wapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.dal.logic.SysDictLogic;
import hw.topevery.basis.entity.fetch.DictFetch;
import hw.topevery.basis.entity.po.SysDict;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "")
@RestController
@RequestMapping("/sysDict")
public class SysDictController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    SysDictLogic sysDictLogic;

/*    @Autowired
    StringRedisTemplate redisTemplate;*/

    @ApiOperation(value = "获取字典列表")
    @GetMapping(value = "/getDictList")
    public JsonResult<?> getDictList(DictFetch para) {
        String key = "sys_dict";
        if (StringUtils.isNotBlank(para.getNsId())) {
            key += ":" + para.getNsId();
        }
        if (StringUtils.isNotBlank(para.getDictName())) {
            key += ":" + para.getDictName();
        }
/*        String dictListJson = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(dictListJson)) {
            List<SysDict> sysDicts = JSON.parseObject(dictListJson, new TypeReference<List<SysDict>>() {
            });
            return JsonResult.ok(sysDicts);
        }*/
        List<SysDict> list = sysDictLogic.getListDict(para);
        String resultJson = "";
        if (CollectionUtils.isNotEmpty(list)) {
            resultJson = JSON.toJSONString(list);
        }
//        redisTemplate.opsForValue().set(key, resultJson, 2L, TimeUnit.MINUTES);
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "字典列表")
    @PostMapping("/list")
    public JsonResult getList(@RequestBody DictFetch dictFetch) {
        JsonResult<DataPager<SysDict>> result = new JsonResult<>(true);
        try {
            result.data = sysDictLogic.getListDictPage(dictFetch);
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
        }
        return result;
    }

    @GetMapping("/listAll")
    public Object getDictList() {
        JsonResult<List<SysDict>> result = new JsonResult<>(true);
        try {
            DictFetch dictFetch = new DictFetch();
            result.data = sysDictLogic.getListDict(dictFetch);
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
        }
        return result;
    }

    @GetMapping("/dictInfo")
    public Object getDictInfo(int id) {
        JsonResult<SysDict> result = new JsonResult<>();
        try {
            SysDict dictInfo = sysDictLogic.getById(id);
            result.data = dictInfo;
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
        }
        return result;
    }

    @PostMapping("/addOrEditDict")
    public JsonResult<Boolean> addOrEditDict(@RequestBody SysDict dictEntity) {
        JsonResult<Boolean> result = new JsonResult<>();
        try {
            SysUser oUser = UserRuntime.getSysUser();
            if (oUser != null) {
                SysDict entity = null;
                if (dictEntity.dictId != null) {
                    entity = new SysDict();
                    entity.dictId = UUID.randomUUID().toString();
                    entity.createId = oUser.userId;
                    entity.createTime = LocalDateTime.now();
                } else {
                    entity = sysDictLogic.getEntityByDataId(dictEntity.dictId);
                    entity.updateId = oUser.userId;
                    entity.updateTime = LocalDateTime.now();
                }
                entity.dictCode = dictEntity.dictCode;
                entity.dictName = dictEntity.dictName;
                entity.remark = dictEntity.remark;
                entity.orderNum = dictEntity.orderNum;
                entity.nsId = dictEntity.nsId;
                result.data = sysDictLogic.save(oUser.id.toString(), entity);
            }
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
//            log.error(ignored.getMessage(), ignored);
        }
        return result;
    }
}
